﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouyei.Geo.GeoParsers.Parser
{
    using Geometries;

    public class PostGISParser:IDisposable
    {
        PostGIS.GeometryReader reader = null;
        PostGIS.GeometryWriter writer = null;

        public GeoType GeometryType { get; private set; }
        public int SRID { get; private set; }
        public PostGISParser()
        {
            
        }

        public virtual Geometry FromReader(byte[] buffer,int offset=0)
        {
            using (reader = new PostGIS.GeometryReader(buffer, offset))
            {
                GeometryType = reader.GeometryType;
                SRID = reader.SRID;
                return reader.Reader();
            }
        }

        public virtual byte[] ToWriter(Geometry geometry)
        {
            using (writer = new PostGIS.GeometryWriter(geometry))
                return writer.Writer();
        }

        public void Dispose()
        {
            if (reader != null)
                reader.Dispose();
            if (writer != null)
            {
                writer.Dispose();
                writer = null;
            }
        }
    }
}
